import { createSlice } from '@reduxjs/toolkit'
import request from '@/utils/request'

export const user = createSlice({
  name: 'usersss',

  initialState: {
    info: {},
  },

  reducers: {
    setUser(state, action) {
      state.info = action.payload
    },
  },
})
// 导出action函数(组件中可以dispatch(action函数)触发reducer更新状态)
export const { setUser } = user.actions
//异步action
export function getUserAction() {
  return async (dispatch, getState) => {
    const { data } = await request.get('user/profile')
    console.log('用户信息:', data)
    dispatch(setUser(data))
  }
}
// 导出reducer(创建store使用)
export default user.reducer
